package com.caiyi.financial.nirvana.sms.subscribe;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.caiyi.financial.nirvana.sms.gateway.Controller;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisPubSub;

import java.lang.reflect.Method;
import java.util.Map;

public class GatewaySubscribeListener extends JedisPubSub {

    private static final Logger logger = LoggerFactory.getLogger(GatewaySubscribeListener.class);
    private static GatewaySubscribeListener instance = new GatewaySubscribeListener();

    private GatewaySubscribeListener() {
    }

    public static GatewaySubscribeListener getInstance() {
        return instance;
    }

    @Override
    public void onMessage(String channel, String message) {
        try {
            if (StringUtils.isBlank(message)) {
                logger.error("GatewaySubscribeListener message is null !!! ");
                return;
            }
            logger.info("GatewaySubscribeListener onMessage message {} ! ", message);
            Map map = JSON.parseObject(message, Map.class);
            String method = map.get("method") + "";
            Method fun = this.getClass().getDeclaredMethod(method, new Class[]{Map.class});
            fun.setAccessible(true);
            fun.invoke(this, map);
        } catch (JSONException exception) {
            logger.info("GatewaySubscribeListener onMessage JSONException {} ! ", message);
        } catch (Exception e) {
            logger.error("GatewaySubscribeListener onMessage error ! ", e);
        }
    }

    private void addGateway(Map map) throws Exception {
        int gatewayId = Integer.parseInt(map.get("gatewayId") + "");
        Controller.initGateWay(gatewayId);
        logger.info("GatewaySubscribeListener addGateway gateway id : {}", gatewayId);
        return;
    }

    private void updateGateway(Map map) throws Exception {
        int gatewayId = Integer.parseInt(map.get("gatewayId") + "");
        logger.info("GatewaySubscribeListener updateGateway gateway id : {}", gatewayId);
        Controller.removeGateWayThread(gatewayId);
        logger.info("GatewaySubscribeListener three seconds after starting gateway id : {}", gatewayId);
        Thread.sleep(3000);
        Controller.initGateWay(gatewayId);
        logger.info("GatewaySubscribeListener start gateway id : {} success", gatewayId);
        return;
    }

    private void deleteGateway(Map map) throws Exception {
        int gatewayId = Integer.parseInt(map.get("gatewayId") + "");
        Controller.removeGateWayThread(gatewayId);
        logger.info("GatewaySubscribeListener deleteGateway gateway id : {}", gatewayId);
        return;
    }

    private void disableGateway(Map map) throws Exception {
        int gatewayId = Integer.parseInt(map.get("gatewayId") + "");
        logger.info("GatewaySubscribeListener disableGateway gateway id : {}", gatewayId);
        Controller.removeGateWayThread(gatewayId);
        return;
    }

    private void enableGateway(Map map) throws Exception {
        int gatewayId = Integer.parseInt(map.get("gatewayId") + "");
        logger.info("GatewaySubscribeListener enableGateway gateway id : {}", gatewayId);
        Controller.initGateWay(gatewayId);
        return;
    }
}
